from db import db
import datetime

class Message(db.Model):
    __tablename__ = 'message'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    from_username = db.Column(db.String(255), nullable=False)
    to_username = db.Column(db.String(255), nullable=False)
    file_url = db.Column(db.String(255), nullable=False)
    content = db.Column(db.String(255), nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.datetime.now)
    status = db.Column(db.Integer, nullable=False)

    # 创建Message时 指定from_status为空 只有当对方回复才给from_status赋值
    def __init__(self,from_username,to_username,file_url,content):
        self.from_username = from_username
        self.to_username = to_username
        self.file_url = file_url
        self.content = content
        self.status = 0

    def to_dict(self):
        return {
            'id': self.id,
            'fromUsername': self.from_username,
            'toUsername': self.to_username,
            'fileUrl': self.file_url,
            'content': self.content,
            'createTime': self.create_time,
            'status': self.status,
        }